import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/model/share_pic_model.dart';
import 'package:flutter_app_unionad/model/task_item_model.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/request/promotion_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/my_facility/widget/select_pay_type_widget.dart';
import 'package:sharesdk_plugin/sharesdk_defines.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'package:sharesdk_plugin/sharesdk_plugin.dart';

class SharePage extends StatefulWidget {
  static const String routeName = '/sharePage';

  // final bool? isSession;

  const SharePage({Key? key}) : super(key: key);

  @override
  _SharePageState createState() => _SharePageState();
}

class _SharePageState extends State<SharePage> {
  bool isShare = false;
  SharePicModel? item;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    PromotionRequest.getShareInfo(type: 3).then((value) {
      if (mounted)
        setState(() {
          item = value;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Scaffold(
        backgroundColor: MyColors.color_245_174_41,
        appBar: MyAppBar(
          context,
          title: '分享',
          backFunction: () {
            Navigator.pop(context, isShare);
          },
          backgroundColor: MyColors.color_245_174_41,
        ),
        body: buildContent(),
      ),
    );
  }

  Widget buildContent() {
    if (item == null) {
      return ProgressDialog.showFirstLoading();
    }
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.px),
      child: Column(
        children: [
          Expanded(
              child: SingleChildScrollView(
            child: Column(
              children: [
                ShowPicWidget(
                  item!.pictureUrl!,
                  width: double.infinity,
                  fit: BoxFit.fitWidth,
                  showDefault: false,
                ),
                SizedBox(height: 20.px),
                Text(
                  item!.title!,
                  style: TextStyle(
                    color: MyColors.color_34_34_34,
                    fontSize: MyFont.font14,
                  ),
                ),
              ],
            ),
          )),
          MySizeBox(height: 20),
          MyButton(
            title: '分享',
            width: 180.px,
            color: Colors.white,
            titleStyle: TextStyle(
              color: MyColors.color_34_34_34,
              fontSize: MyFont.font17,
            ),
            radius: 20,
            onTap: () {
              //
              shareOnTap();
            },
          ),
          SafeArea(child: MySizeBox(height: 20)),
        ],
      ),
    );
  }

  void shareOnTap() {
    ShowBottomAlertDialog.showBottomDialog(
      context,
      builder: (ctx) {
        return SelectPayTypeWidget(
          title: '选择分享方式',
          icons: ['wx_1', 'wechat_moments'],
          weChatPayOnTop: () {
            //
            share(true);
          },
          alipayPayOnTop: () {
            //
            share(false);
          },
        );
      },
      height: 168.px + SizeFill.statusHeight!,
    );
  }

  void share(bool isSession) {
    isShare = true;
    // return;
    SSDKMap ssdkMap = SSDKMap()
      ..setGeneral(
          //title	标题（String）
          item!.title!,
          // text	文本（String）
          "",
          //images	图片数组ios使用（dynamic）
          [item!.pictureUrl!],
          //imageUrlAndroid	url图片地址（String）
          "",
          "",
//          "${imageUrl}",
          //url	url地址（String）
          "${Configuration.h5Share}${FacilityRequest.encodeBase64(UserCacheUil.getString(Configuration.mobile) ?? "")}",
          //右侧图片地址
          item!.pictureUrl!,
          //musicUrlAndroid	音乐url地址（String）
          "",
          //videoUrlAndroid	视频url地址（String）
          "",
          //filePath	文件地址（String）
          "",
          //分享类型
          SSDKContentTypes.webpage);
    SharesdkPlugin.share(isSession ? ShareSDKPlatforms.wechatSession : ShareSDKPlatforms.wechatTimeline, ssdkMap,
        (state, map1, map2, err) {
      //
      if (state == SSDKResponseState.Success) {}
    });
  }
}
